package com.terran4j.kotlin.hello;

/**
 * Java 的单例模式一共有 7 种写法
 * 这里是常用的“双重检查锁定”方式实现的单例
 */
public class SingletonJava {

    private volatile static SingletonJava singleton;

    private SingletonJava() {
    }

    public static SingletonJava getSingleton() {
        if (singleton == null) {
            synchronized (SingletonJava.class) {
                if (singleton == null) {
                    singleton = new SingletonJava();
                }
            }
        }
        return singleton;
    }

    private int counter = 0;

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }

}